package com.example.webpractice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.webpractice.po.XueshengchengjiPO;
import com.example.webpractice.service.XueshengchengjiService;
import com.example.webpractice.utils.ServerResponseEntity;
import com.example.webpractice.vo.XueshengchengjiDetailVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 学生成绩
 * 后端接口
 */
@RestController
@RequestMapping("/xueshengchengji")
public class XueshengchengjiController {
    @Autowired
    private XueshengchengjiService xueshengchengjiService;

    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public ServerResponseEntity page(@RequestParam(name = "page") Integer page,
                                     @RequestParam(name = "limit") Integer limit,
                                     @RequestParam(name = "kechengmingcheng", required = false) String kechengmingcheng,
                                     @RequestParam(name = "chengjimingcheng", required = false) String chengjimingcheng,
                                     @RequestParam(name = "jiaoshixingming", required = false) String jiaoshixingming,
                                     @RequestParam(name = "xueshengxingming", required = false) String xueshengxingming,
                                     HttpServletRequest request) {
        QueryWrapper<XueshengchengjiPO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(kechengmingcheng)) {
            queryWrapper.like("kechengmingcheng", kechengmingcheng);
        }
        if (StringUtils.isNotBlank(chengjimingcheng)) {
            queryWrapper.like("chengjimingcheng", chengjimingcheng);
        }
        if (StringUtils.isNotBlank(jiaoshixingming)) {
            queryWrapper.like("jiaoshixingming", jiaoshixingming);
        }
        if (StringUtils.isNotBlank(xueshengxingming)) {
            queryWrapper.like("xueshengxingming", xueshengxingming);
        }
        String tableName = (String) request.getAttribute("tableName");
        String username;
        if (tableName.equals("jiaoshi")) {
            username = (String) request.getAttribute("username");
            queryWrapper.eq("gonghao", username);
        }
        tableName = request.getAttribute("tableName").toString();
        if (tableName.equals("xuesheng")) {
            queryWrapper.eq("xuehao", request.getAttribute("username"));
        }
        return ServerResponseEntity.success(xueshengchengjiService.queryPage(page, limit, queryWrapper));
    }

    /**
     * 详情
     */
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Long id) {
        XueshengchengjiDetailVO vo = xueshengchengjiService.selectById(id);
        return ServerResponseEntity.success(vo);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Long[] ids) {
        if (xueshengchengjiService.deleteBatchIds(Arrays.asList(ids)) != ids.length) {
            return ServerResponseEntity.fail(-1, "删除失败");
        }
        return ServerResponseEntity.success();
    }
}